package online.sh.cn.fy.common.servlet;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;
import online.sh.cn.fy.common.api.SqlUtil;
import online.sh.cn.fy.common.api.WxApi;
import online.sh.cn.fy.common.utils.CookieUtil;
import online.sh.cn.fy.common.utils.sign;

/**
 * 生成页面jsapi
 * 并换取认证jsticket
 */
@WebServlet("/checkJsapi")
public class checkJsapi extends HttpServlet {
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		String url=request.getParameter("url");
		SqlUtil sqlutil= new SqlUtil();
		WxApi wxutil= new WxApi();
		String jsapi_ticket=sqlutil.getJsTicket();
		String time=sqlutil.getJsTicketTime();
		String token=sqlutil.getAccesstoken();
		if (evaltime(time)>3600) {
			JSONObject obj=JSONObject.fromObject(wxutil.getJsTicket(token));
			//System.out.println("更新jsticket:"+obj.toString());
			if ((int)obj.get("errcode")==0) {
				sqlutil.updateJsTicket(obj.get("ticket").toString());
				//System.out.println("token未过期");
			}else{
				token=JSONObject.fromObject(wxutil.getAccessToken()).getString("access_token").toString();
				jsapi_ticket=JSONObject.fromObject(wxutil.getJsTicket(token)).get("ticket").toString();
				sqlutil.updateAccessToken(token);
				sqlutil.updateJsTicket(jsapi_ticket);
				//System.out.println("更新了jsticket："+jsapi_ticket);
				//System.out.println("更新了wxtoken："+token);
			}
		}
		String openid=CookieUtil.getCookieValueByName(request, "openid");
		Map<String,String> map=sign.sign(jsapi_ticket, url);
		map.put("openid", openid);
		JSONObject object=JSONObject.fromObject(map);
//		System.out.println(object.toString());
		render(response, "application/json;charset=UTF-8", object.toString());
	}
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}
	
	protected static void render(HttpServletResponse response, String contentType,String text) {
		response.setContentType(contentType);
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		try {
			response.getWriter().write(text);
		} catch (IOException e) {
			
		}
	}
	
	//判断时间差，7200s
	protected static long evaltime(String time) {
		Date nowtime=new Date();
		Date extime=null;
		try {
			extime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(time);
		} catch (ParseException e) {
			
		}
		long evaltime=((nowtime.getTime()-extime.getTime())/1000);
		return evaltime;
	}
	
	public static void main(String[] args) {
		System.out.println(evaltime("2017-07-13 14:03:02"));
	}
}
